#include "Server.hpp"
#include <memory>
void Get(const Request &req, Response &resp)
{
    cout << "----------------------http recv start---------------------------" << endl;
    cout << req.inbuffer << std::endl;
    std::cout << "method: " << req.method << std::endl;
    std::cout << "url: " << req.url << std::endl;
    std::cout << "httpversion: " << req.httpversion << std::endl;
    std::cout << "path: " << req.path << std::endl;
    cout << "----------------------http recv end---------------------------" << endl;

    cout << "----------------------http send start---------------------------" << endl;
    resp.outbuffer += "HTTP/1.1 200 OK\r\n";
    resp.outbuffer += "Content-Type: text/html\r\n";
    resp.outbuffer += "\r\n";
    string body;
    body.resize(4096);
    if (!Util::readFile(req.path, (char *)body.c_str(), body.size()))
        !Util::readFile("./wwwroot/404.html", (char *)body.c_str(), body.size());
    cout << "test : -----------" << req.path << endl;
    // cout << body << endl;
    resp.outbuffer += body;
    cout << resp.outbuffer << endl;
    cout << "----------------------http send end---------------------------" << endl;
}
int main(int argc, char *argv[])
{
    while (argc != 2)
    {
        cerr << "请重新输入" << endl;
    }

    uint16_t port = atoi(argv[1]);
    unique_ptr<Server> usvr(new Server(port));
    usvr->init();
    usvr->start(Get);
}